var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el: '#pmpapp',
    data: {
        showList: true,
        title: null,

        menu: {
            parentName: null,
            parentId: 0,
            type: 1,
            orderNum: 0
        }
    },
    methods: {
        //新增
        getMenu: function (menuId) {
            //加载菜单树
            $.get(baseURL + "permissions/select", function (r) {
                ztree = $.fn.zTree.init($("#menuTree"), setting, r.data.menuList);
                var node = ztree.getNodeByParam("menuId", vm.menu.parentId);
                ztree.selectNode(node);

                vm.menu.parentName = node.name;
            })
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.menu = {parentName: null, parentId: 0, type: 1, orderNum: 0};
            vm.getMenu();
        },
        //修改
        update: function () {
            var id = getMenuId();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            alert(id);

            $.get(baseURL + "permissions/info/" + id, function (r) {
                vm.showList = false;
                vm.title = "修改";
                vm.menu = r.data.menu;
                vm.getMenu();
            });
        },

        del: function () {
            var menuId = getMenuId();
            if (menuId == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "permissions/delete",
                    data: "menuId=" + menuId,
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },

        saveOrUpdate: function () {
            if (vm.validator()) {
                return;
            }

            var url = vm.menu.menuId == null ? "permissions/save" : "permissions/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.menu),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        menuTree: function () {
            layer.open({
                type: 1,
                offset: '30px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '300px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.menu.parentId = node[0].id;
                    vm.menu.parentName = node[0].name;

                    layer.close(index);
                }
            });
        },

        reload: function () {
            vm.showList = true;
            Menu.table.refresh();
        },

        validator: function () {
            if (isBlank(vm.menu.name)) {
                alert("菜单名称不能为空");
                return true;
            }

            //菜单
            if (vm.menu.type === 1 && isBlank(vm.menu.url)) {
                alert("菜单链接url不能为空");
                return true;
            }
        }
    }
});


var Menu = {
    id: "menuTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Menu.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'Id', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '父级菜单', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '排序编号', field: 'orderNum', align: 'center', valign: 'middle', sortable: true, width: '70px'},
        {title: '图标',field: 'icon',align: 'center',valign: 'middle',sortable: true,width: '50px',
            formatter: function (item, index) {

                return item.icon == null ? '' : '<i class="' + item.icon + ' fa-lg"></i>';
            }
        },
        {title: '类型',field: 'type',align: 'center',valign: 'middle',sortable: true,width: '80px',
            formatter: function (item, index) {

                if (item.type === 0) {
                    return '<span class="label label-primary">目录</span>';
                }
                if (item.type === 1) {
                    return '<span class="label label-success">菜单</span>';
                }
                if (item.type === 2) {
                    return '<span class="label label-warning">按钮</span>';
                }
            }
        },
        {title: '菜单链接url', field: 'url', align: 'center', valign: 'middle', sortable: true, width: '150px'},
        {title: '授权编码', field: 'perms', align: 'center', valign: 'middle', sortable: true, width: '150px'},
        {title: '创建人', field: 'createname', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '备注', field: 'remark', align: 'center', valign: 'middle', sortable: true}
    ]
    return columns;
};


function getMenuId() {
    var selected = $('#menuTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}


$(function () {
    var colunms = Menu.initColumn();
    var table = new TreeTable(Menu.id, baseURL + "permissions/list", colunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(true);
    table.init();
    Menu.table = table;
});
